package com.example.canalclientsync.canal.handler;

import com.example.canalclientsync.canal.po.Project;
import com.example.canalclientsync.canal.po.ProjectMember;
import com.example.canalclientsync.canal.service.ModelService;
import com.example.canalclientsync.es.document.EsModel;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchPhraseQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
//import top.javatool.canal.client.annotation.CanalTable;
//import top.javatool.canal.client.handler.EntryHandler;

import javax.annotation.Resource;
import java.util.List;

/**
 * 更新项目成员
 *
 * @author chen.cr
 * @since 2023/9/20
 */
//@Component
//@CanalTable("t_project_user_rel")
@Slf4j
public class ProjectUserRelationHandler implements EntryHandler<ProjectMember> {

    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Resource
    private ModelService modelService;


    @Override
    public void insert(ProjectMember projectMember) {
        log.info("insert ProjectMember");
        this.doUpdate0(projectMember);
        log.info("Es更新项目相关信息成功");
    }

    @Override
    public void update(ProjectMember before, ProjectMember after) {
        log.info("update ProjectMember");
        this.doUpdate0(after);
        log.info("Es更新项目相关信息成功");
    }

    @Override
    public void delete(ProjectMember projectMember) {
        log.info("delete ProjectMember");
        this.doUpdate0(projectMember);
        log.info("Es更新项目相关信息成功");
    }


    /**
     * 修改项目成员后，更新项目相关模型数据
     *
     * @param projectMember
     */
    private void doUpdate0(ProjectMember projectMember) {
        // 查询项目下的所有模型数据
        MatchPhraseQueryBuilder matchPhraseQueryBuilder = QueryBuilders.matchPhraseQuery("tenantId", projectMember.getTenantId());
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("belongedProject.id", projectMember.getProjectId());
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.must(matchPhraseQueryBuilder);
        boolQueryBuilder.must(termQueryBuilder);
        NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(boolQueryBuilder);
        SearchHits<EsModel> hits = elasticsearchRestTemplate.search(nativeSearchQuery, EsModel.class);

        if (hits.hasSearchHits()) {
            // 查询project相关数据
            Project project = modelService.searchProjectObject(projectMember.getProjectId(), projectMember.getTenantId());
            // project.getCreateTime().
            List<UpdateQuery> uqs = Lists.newArrayList();
            for (SearchHit<EsModel> hit : hits.getSearchHits()) {
                if (hit.getId() != null) {
                    UpdateQuery updateQuery = UpdateQuery.builder(hit.getId()).withDocument(project.getDocument("belongedProject")).build();
                    uqs.add(updateQuery);
                }
            }
            if (uqs.size() > 0) {
                elasticsearchRestTemplate.bulkUpdate(uqs, EsModel.class);
            }
        }
    }

}
